package com.extend;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

//@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("MyBeanDefinitionRegistryPostProcessor.postProcessBeanFactory()>>>");
		System.out.println("打印Bean的数量：>" + beanFactory.getBeanDefinitionCount());
		String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
		for (String name : beanDefinitionNames) {
			System.out.println("打印Bean的名称：》》》  " +name);
		}
	}

	// BeanDefinitionRegistry:
	// Bean定义信息的所有保存中心，以后BeanFactory解释按照BeanDefinitionRegistry里面保存的每一个bean定义信息创建Bean的实例
	// 该方法先执行
	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		System.out.println("MyBeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry()>>>***");
		System.out.println("打印Bean的数量：>" + registry.getBeanDefinitionCount());
		BeanDefinition beanDefinition = new RootBeanDefinition(Person.class);
		registry.registerBeanDefinition("person", beanDefinition);
	}

}
